<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;


use App\Common\Logger;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class MiGuPayCallback
{

    public function callback(Request $request)
    {
        Logger::instance()->info('支付回调', $request->getUri());
        $data = json_decode($request->get('para'), true);
        if (!isset($data['order_id']) || !isset($data['status'])) {
            Logger::instance()->error('MiguChargeCallback', '缺少order_id参数或status参数');
            return ['code' => -6, 'msg' => '缺少order_id参数或status参数'];
        }
        $status = $data['status'] == 0 ? 1 : -1;

        if (DB::table('t_pay_info')->where('order_id', $data['order_id'])->where('order_status', $status)->value('id')) {
            return ['code' => -9, 'msg' => '已经处理成功'];
        }

        $update = [
            'pay_time' => date('Y-m-d H:i:s'),
            'order_status' => $status,
            'desc' => $data['msg'] ?? ''
        ];

        $result = DB::table('t_pay_info')
            ->where('order_id', $data['order_id'])
            ->where('order_status', 0)
            ->update($update);
        if ($result) {
            if ($status == -1) {
                return ['code' => -7, 'msg' => '第三方支付返回结果失败'];
            }
            return ['code' => 200, 'msg' => '处理成功'];
        }
        return ['code' => -8, 'msg' => '处理失败'];
    }

}